<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      myData: []
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(val) {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(val)
    },
    setOptions({ proportion, meaning } = {}) {
      // 1, 1, 1, 0,  //(上-下 渐变)
      // 1, 1, 0, 0,  //(左上-右下 渐变)
      // 1, 0, 0, 0,  //(左-右 渐变)
      // 0, 1, 1, 0,  //(右上-左下 渐变)
      // 0, 1, 1, 1,  //(左-右 渐变)
      // 1, 0, 1, 1,  //(上-下 渐变)
      // 0, 0, 1, 0,  //(左-右 渐变)
      // 0, 0, 0, 1, // (上-下 渐变)
      var data = []
      if (proportion.length === 1) {
        this.myData = [
          {
            value: proportion[0].value,
            name: proportion[0].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F0961B ' },
                    { offset: 1, color: '#FE5B54' }
                  ]
                )
              }
            }
          }
        ]
      } else if (proportion.length === 2) {
        this.myData = [
          {
            value: proportion[0].value,
            name: proportion[0].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F370CE ' },
                    { offset: 1, color: '#ef91c4' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[1].value,
            name: proportion[1].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#FC0648' },
                    { offset: 1, color: '#F9507D' }
                  ]
                )
              }
            }
          }
        ]
      } else if (proportion.length === 3) {
        this.myData = [
          {
            value: proportion[0].value,
            name: proportion[0].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#FC0648' },
                    { offset: 1, color: '#F9507D' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[1].value,
            name: proportion[1].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F0961B ' },
                    { offset: 1, color: '#FE5B54' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[2].value,
            name: proportion[2].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#72b2ff' },
                    { offset: 1, color: '#49B0FE' }
                  ]
                )
              }
            }
          }
        ]
      } else if (proportion.length === 4) {
        this.myData = [
          {
            value: proportion[0].value,
            name: proportion[0].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F370CE ' },
                    { offset: 1, color: '#ef91c4' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[1].value,
            name: proportion[1].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#FC0648' },
                    { offset: 1, color: '#F9507D' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[2].value,
            name: proportion[2].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F0961B ' },
                    { offset: 1, color: '#FE5B54' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[3].value,
            name: proportion[3].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#72b2ff' },
                    { offset: 1, color: '#49B0FE' }
                  ]
                )
              }
            }
          }
        ]
      } else if (proportion.length === 5) {
        this.myData = [
          {
            value: proportion[0].value,
            name: proportion[0].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F370CE ' },
                    { offset: 1, color: '#ef91c4' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[1].value,
            name: proportion[1].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#FC0648' },
                    { offset: 1, color: '#F9507D' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[2].value,
            name: proportion[2].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#F0961B ' },
                    { offset: 1, color: '#FE5B54' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[3].value,
            name: proportion[3].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#72b2ff' },
                    { offset: 1, color: '#49B0FE' }
                  ]
                )
              }
            }
          },
          {
            value: proportion[4].value,
            name: proportion[4].name,
            itemStyle: {
              normal: { // 颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, // (上-下 渐变)
                  [
                    { offset: 0, color: '#9005FB' },
                    { offset: 1, color: '#D2A0F9' }
                  ]
                )
              }
            }
          }
        ]
      } else {
        this.myData = proportion
      }

      data = this.myData
      this.chart.setOption({
        title: {
          text: ''
        },
        animation: true,
        animationDuration: 1500,
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          data: meaning
        },
        label: {
          alignTo: 'edge',
          formatter: function(params) {
            return '{name|' + params.name + '} ' + params.percent + '%\n{time|' + params.value + ' 元}'
          },
          minMargin: 6,
          edgeDistance: 15,
          lineHeight: 20,
          rich: {
            time: {
              fontSize: 12,
              color: '#999'
            }
          }
        },
        series: [
          {
            name: '项目 金额 占比率',
            type: 'pie',
            radius: [20, 95],
            data,
            animationEasing: 'cubicInOut'
          }
        ]
      })
    }
  }
}
</script>
